﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace Com.Tom.CdcCdcCommon
{
    public enum SplashType
    {
        Viewer,
        Manager
    }

    public partial class SplashScreen : Form
    {
        Timer timer;
        int dispPeriod;

        public SplashScreen(SplashType type, int displaySeconds)
        {
            dispPeriod = displaySeconds;
            InitializeComponent();
            switch (type)
            {
                case SplashType.Manager:
                    uxBkgPicture.Image = CdcCommon.Properties.Resources.Manager;
                    break;
                case SplashType.Viewer:
                    uxBkgPicture.Image = CdcCommon.Properties.Resources.Viewer;
                    break;
                default:
                    throw new NotSupportedException("Provided SplashType is not supported.");
            }
            if (displaySeconds == int.MaxValue)
            {
                uxClose.Visible = true;
                uxLicense.Visible = true;
            }
            else
            {
                uxClose.Visible = false;
                uxLicense.Visible = false;
            }
            uxExpiryMsg.Visible = false;
        }

        private void SplashScreen_Shown(object sender, EventArgs e)
        {
            if (dispPeriod != int.MaxValue)
            {
                if (DateTime.Now > Com.TomCdc.ChangeDataCapture.CommonHelper.ExpiryDate)
                {
                    uxExpiryMsg.Visible = true;
                    uxClose.Visible = true;
                    uxClose.Enabled = false;
                    int cDown = 30;
                    while (cDown > 0)
                    {
                        uxClose.Text = "Close (" + cDown.ToString() + "s)";
                        Application.DoEvents();
                        System.Threading.Thread.Sleep(1000);
                        cDown--;
                    }
                    uxClose.Text = "Close";
                    uxClose.Enabled = true;
                }
                else
                {
                    timer = new Timer();
                    timer.Tick += DisplayElapsed;
                    timer.Interval = dispPeriod * 1000;
                    timer.Enabled = true;
                    timer.Start();
                }
            }
        }


        private void DisplayElapsed(object obj, EventArgs args)
        {
            timer.Stop();
            Close();
        }

        private void uxClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void uxLicense_Click(object sender, EventArgs e)
        {
            Process.Start("notepad.exe", Path.Combine(Application.StartupPath, "license.txt"));
        }

        private void uxExpiryMsg_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.tomcdc.co.cc");
        }
    }
}
